package com.xysq.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.rockcent.action.CallbackListener;
import com.rockcent.model.CustCouponOrderBO;
import com.umeng.analytics.MobclickAgent;
import com.xysq.activity.TipsActivity;
import com.xysq.adapter.MyFinishOrderListAdapter;
import com.xysq.lemon.R;
import com.xysq.util.ToastUtil;
import com.xysq.util.UserInfoKeeper;
import com.xysq.widget.LoadData;
import com.xysq.widget.ScrollListener;

import java.util.List;

public class MyFinishOrderListFragment extends BaseFragment {

    public static final int PAGE_RECEIVE = 0; //接到的单
    public static final int PAGE_ORDER = 1; //我下的单

    public static final int TYPE_PENDING = 0;
    public static final int TYPE_FINISHED = 1;
    public static final int TYPE_CENCELED = 2;

    private static final String PARAM_TYPE = "type";
    private static final String PARAM_PAGE = "page";

    private int type;
    private int page;
    private TextView emptyTxt;
    private ProgressBar progressBar;
    private SwipeRefreshLayout swipeRefreshLayout;
    private ListView orderList;
    private MyFinishOrderListAdapter orderListAdapter;
    private String customerId;
    ScrollListener scrollListener;

    public static MyFinishOrderListFragment newInstance(int type, int page) {
        MyFinishOrderListFragment fragment = new MyFinishOrderListFragment();
        Bundle args = new Bundle();
        args.putInt(PARAM_TYPE, type);
        args.putInt(PARAM_PAGE, page);
        fragment.setArguments(args);
        return fragment;
    }

    public MyFinishOrderListFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt(PARAM_TYPE, 0);
            page = getArguments().getInt(PARAM_PAGE, 0);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_order_list, container, false);
        emptyTxt = (TextView) rootView.findViewById(R.id.txt_empty);
        progressBar = (ProgressBar) rootView.findViewById(R.id.progress_loading);
        swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                scrollListener.setCurrentPage(1);
                orderListAdapter.clearItems();
                loadData();
            }
        });
        orderList = (ListView) rootView.findViewById(R.id.list_order);
        orderListAdapter = new MyFinishOrderListAdapter(this, getActivity(), appAction, page, type);
        orderList.setAdapter(orderListAdapter);
        scrollListener = new ScrollListener(orderList, true, new LoadData() {
            @Override
            public void loadMyData() {
                loadData();
            }
        });
        orderList.setOnScrollListener(scrollListener);
        orderList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                CustCouponOrderBO custCouponOrderBO = (CustCouponOrderBO) orderListAdapter.getItem(position);
                Intent intent = new Intent(getActivity(), TipsActivity.class);
                intent.putExtra(TipsActivity.COUPONMODEL_ID, custCouponOrderBO.getCouponId());
                intent.putExtra(TipsActivity.CUSTOMER_ID, custCouponOrderBO.getCustomerId());
                startActivity(intent);
            }
        });
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        customerId = UserInfoKeeper.readCustomerId(getActivity());
    }

    public void reloadData() {
        scrollListener.setCurrentPage(1);
        orderListAdapter.clearItems();
        loadData();
    }

    public void loadData() {
        emptyTxt.setVisibility(View.GONE);
        progressBar.setVisibility(View.VISIBLE);
        if (page == PAGE_RECEIVE) {
            appAction.listMyReceiveCoupons(customerId, type, scrollListener.getCurrentPage(), new CallbackListener<List<CustCouponOrderBO>>() {
                @Override
                public void onSuccess(List<CustCouponOrderBO> data) {
                    if (scrollListener.getCurrentPage() == 1) {
                        if (data.size() == 0) {
                            emptyTxt.setVisibility(View.VISIBLE);
                        } else {
                            emptyTxt.setVisibility(View.GONE);
                            orderListAdapter.clearItems();
                            orderListAdapter.setItems(data);
                        }
                    } else {
                        emptyTxt.setVisibility(View.GONE);
                        if (data.size() > 0) {
                            orderListAdapter.addItems(data);
                            scrollListener.setLoadFinish(true);
                        }
                    }
                    swipeRefreshLayout.setRefreshing(false);
                    progressBar.setVisibility(View.GONE);
                }

                @Override
                public void onFailure(String errorCode, String message) {
                    ToastUtil.showShort(getActivity(), message);
                    swipeRefreshLayout.setRefreshing(false);
                    progressBar.setVisibility(View.GONE);
                    scrollListener.setLoadFinish(true);
                }
            });
        } else {
            final int currentPage = scrollListener.getCurrentPage();
            appAction.listMyOrders(type, customerId, currentPage, new CallbackListener<List<CustCouponOrderBO>>() {
                @Override
                public void onSuccess(List<CustCouponOrderBO> data) {
                    if (currentPage == 1) {
                        if (data.size() == 0) {
                            emptyTxt.setVisibility(View.VISIBLE);
                        } else {
                            orderListAdapter.clearItems();
                            emptyTxt.setVisibility(View.GONE);
                            orderListAdapter.setItems(data);
                        }
                    } else {
                        emptyTxt.setVisibility(View.GONE);
                        if (data.size() > 0) {
                            orderListAdapter.addItems(data);
                            scrollListener.setLoadFinish(true);
                        }
                    }
                    swipeRefreshLayout.setRefreshing(false);
                    progressBar.setVisibility(View.GONE);
                }

                @Override
                public void onFailure(String errorCode, String message) {
                    ToastUtil.showShort(getActivity(), message);
                    swipeRefreshLayout.setRefreshing(false);
                    progressBar.setVisibility(View.GONE);
                    scrollListener.setLoadFinish(true);
                }
            });
        }
    }

    @Override

    public void onResume() {
        super.onResume();
        reloadData();
        if (page == PAGE_ORDER) {
            if (type == TYPE_PENDING) {
                MobclickAgent.onPageStart("我下的单页已付款");
            }
            if (type == TYPE_FINISHED) {
                MobclickAgent.onPageStart("我下的单页已完成");
            }
            if (type == TYPE_CENCELED) {
                MobclickAgent.onPageStart("我下的单页已取消");
            }
        }
        if (page == PAGE_RECEIVE) {
            if (type == TYPE_PENDING) {
                MobclickAgent.onPageStart("我接到的单待处理");
            }
            if (type == TYPE_FINISHED) {
                MobclickAgent.onPageStart("我接到的单已完成");
            }
            if (type == TYPE_CENCELED) {
                MobclickAgent.onPageStart("我接到的单已取消");
            }
        }
    }

    @Override
    public void onPause() {
        super.onPause();
//        MobclickAgent.onPageEnd("编辑个人信息页");
        if (page == PAGE_ORDER) {
            if (type == TYPE_PENDING) {
                MobclickAgent.onPageEnd("我下的单页已付款");
            }
            if (type == TYPE_FINISHED) {
                MobclickAgent.onPageEnd("我下的单页已完成");
            }
            if (type == TYPE_CENCELED) {
                MobclickAgent.onPageEnd("我下的单页已取消");
            }
        }
        if (page == PAGE_RECEIVE) {
            if (type == TYPE_PENDING) {
                MobclickAgent.onPageEnd("我接到的单待处理");
            }
            if (type == TYPE_FINISHED) {
                MobclickAgent.onPageEnd("我接到的单已完成");
            }
            if (type == TYPE_CENCELED) {
                MobclickAgent.onPageEnd("我接到的单已取消");
            }
        }
    }
}
